package com.feishi.core.util.common;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: caixq
 * Date: 2020-08-10
 * Time: 上午11:04
 */
public class ResizeImg {
    public static void main(String[] args) {
//        resizeImages("/home/caixq/Desktop/微笑照片收集（8-7）","/home/caixq/Desktop/输出");
        String name="龙创（常州）2018.jpeg";

        writeNameOnImages("/home/caixq/Desktop/新建文件夹","/home/caixq/Desktop/水印1");

    }

    private static void writeNameOnImages(String baseDir, String outDir) {
        FileUtil.ergodic(new File(baseDir),".*("+"png|jpeg|jpg"+").*",file->{
            try {
                BufferedImage originalImage = ImageIO.read(file);
//                BufferedImage out=resizeImage(originalImage,480,480);
                String outPath= file.getPath().replace(baseDir,outDir).replace(" ","");

                String content=file.getName().split("\\.")[0].replaceAll("（","(").replace("）",")").replace(" ","");


                File fs=new File(outPath);
                fs.getParentFile().mkdirs();
                int width=originalImage.getWidth();
                int height=originalImage.getHeight();

                BufferedImage bufImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                Graphics2D g = bufImg.createGraphics();
                g.drawImage(originalImage, 0, 0, width, height, null);
                int fontSize=height/20;


                Font font = new Font("Serif", Font.BOLD, fontSize);

//                g.setBackground(Color.WHITE);
//                g.clearRect(0, 0, width, height);



                g.setFont(font);

                FontRenderContext context = g.getFontRenderContext();
                Rectangle2D bounds = font.getStringBounds(content, context);
                if(bounds.getWidth()>width){
                    font = new Font("Serif", Font.BOLD, (int) (fontSize*1.0/bounds.getWidth()*width)-1);
                    g.setFont(font);
                    bounds = font.getStringBounds(content, context);
                }
                double x = (width - bounds.getWidth()) / 2;
                double y = (height - bounds.getHeight());
                double ascent = -bounds.getY();
                double baseY = y + ascent;
                g.setPaint(new Color(157, 111, 60));
                g.drawString(content, (int)x, (int)baseY);

                g.setPaint(new Color(177, 138, 80));
                g.drawString(content, (int)x-1, (int)baseY-1);

                g.drawString(content, (int)x-2, (int)baseY-2);
                g.setPaint(new Color(197, 158, 100));

                g.drawString(content, (int)x-3, (int)baseY-3);

                g.setPaint(new Color(229, 195, 121));
                g.drawString(content, (int)x-4, (int)baseY-4);




                ImageIO.write(bufImg, file.getName().split("\\.")[file.getName().split("\\.").length-1],new File(outPath));
            } catch (IOException e) {
                e.printStackTrace();
            }


        });

    }

    private static void resizeImages( String baseDir,String outDir) {


        FileUtil.ergodic(new File(baseDir),".*("+"png|jpeg|jpg"+").*",file->{
            try {
                BufferedImage originalImage = ImageIO.read(file);
                BufferedImage out=resizeImage(originalImage,480,480);
                String outPath= file.getPath().replace(baseDir,outDir);
                File fs=new File(outPath);
                fs.getParentFile().mkdirs();


                ImageIO.write(out, file.getName().split("\\.")[file.getName().split("\\.").length-1],new File(outPath));
            } catch (IOException e) {
                e.printStackTrace();
            }


        });

    }

    private static BufferedImage resizeImage(BufferedImage originalImage,int maxWidth,int maxHeight){

        int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
        if(originalImage.getWidth()/maxWidth>originalImage.getHeight()/maxHeight){
            maxHeight= (int) (originalImage.getHeight()*1.0/originalImage.getWidth()*maxWidth);
        }else {
            maxWidth= (int) (originalImage.getWidth()*1.0/originalImage.getHeight()*maxHeight);
        }

        BufferedImage resizedImage = new BufferedImage(maxWidth, maxHeight, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, maxWidth, maxHeight, null);
        if(maxWidth>maxHeight){
            //旋转
//            g.rotate(90);
        }
        g.dispose();

        return resizedImage;
    }

}
